<%@ page contentType="text/html; charset=utf-8" %>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring" %>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<%@taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<c:set var="basePath" value="${pageContext.request.contextPath}"/>
<!DOCTYPE HTML>
<html lang="zh-cn">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>岗位管理</title>
    <jsp:include page="/resources/inc/head.jsp"/>
    <link href="${basePath}/resources/zheng-admin/plugins/zTree_v3/css/metroStyle/metroStyle.css" rel="stylesheet"/>
</head>
<body>
<aside id="iframe_aside" class="sidebar1">
    <div class="zTreeDemoBackground left">
        <ul id="aside_tree" class="ztree"></ul>
    </div>
</aside>
<div id="main">
    <div id="toolbar">
        <shiro:hasPermission name="oa:hr:position:create">
            <a class="waves-effect waves-button" href="javascript:;"
               onclick="Common.openPage({url:'${basePath}/oa/hr/position/create',title:'新增岗位',param:getChecked()})">
                <i class="zmdi zmdi-plus"></i>新增岗位
            </a>
        </shiro:hasPermission>
        <shiro:hasPermission name="oa:hr:position:delete">
            <a class="waves-effect waves-button" href="javascript:;"
               onclick="Common.batchDelete({url:'${basePath}/oa/hr/position/delete',afterDelete:refreshTree})">
                <i class="zmdi zmdi-close"></i>删除岗位
            </a>
        </shiro:hasPermission>
    </div>
    <table id="table"></table>
</div>
<jsp:include page="/resources/inc/footer.jsp"/>
<script>
    var query = {};
    var $table = $('#table');
    var zNodes = [];
    var treeObj;
    var checkedNode;

    $(function () {
        // bootstrap table初始化
        $table.bootstrapTable({
            url: '${basePath}/oa/hr/position/list',
            height: getHeight(),
            striped: true,//隔行变色
            search: true,
            showRefresh: true,
            showColumns: true,//是否显示 内容列下拉框
            minimumCountColumns: 2,//当列数小于此值时，将隐藏内容列下拉框
            clickToSelect: true,//设置true 将在点击行时，自动选择rediobox 和 checkbox
            detailView: true,//设置为 true 可以显示详细页面模式。
            detailFormatter: 'detailFormatter',//格式化详细页面模式的视图。
            pagination: true,//设置为 true 会在表格底部显示分页条
            paginationLoop: false,//设置为 true 启用分页条无限循环的功能。
            sidePagination: 'server',
            silentSort: false,//设置为 false 将在点击分页按钮时，自动记住排序项。仅在 sidePagination设置为 server时生效.
            smartDisplay: false,//True to display pagination or card view smartly.
            escape: true,//转义HTML字符串，替换 &, <, >, ", `, 和 ' 字符.
            searchOnEnterKey: true,//设置为 true时，按回车触发搜索方法，否则自动触发搜索方法
            idField: 'id',//指定主键列
            maintainSelected: true,//设置为 true 在点击分页按钮或搜索按钮时，将记住checkbox的选择项
            toolbar: '#toolbar',
            queryParamsType: "",
            queryParams: function (params) {
                params.id = query.id;
                return params;
            },
            columns: [
                {field: 'ck', checkbox: true},
                {field: 'id', title: '编号', sortable: true, align: 'center'},
                {field: 'name', title: '岗位名称', align: 'center'},
                {field: 'ctime', title: '创建日期', align: 'center', formatter: 'timeFormatter'},
                {
                    field: 'action',
                    title: '操作',
                    align: 'center',
                    formatter: 'actionFormatter',
                    events: 'actionEvents',
                    clickToSelect: false
                }
            ]
        });
        updateZTree();
        checkedNode = zNodes[0];
        treeObj = $.fn.zTree.init($("#aside_tree"), setting, zNodes);
    });
    // 格式化操作按钮
    var updateData = {
        title: "编辑岗位",
        url: "${basePath}/oa/hr/position/update",
    };
    var deleteData = {
        url: "${basePath}/oa/hr/position/delete",
        afterDelete: 'refreshTree'
    };
    // 格式化操作按钮
    function actionFormatter(value, row, index) {
        var html = "";
        updateData.param = Common.getParam({id: row.id});
        deleteData.id = row.id;
        <shiro:hasPermission name="oa:hr:position:update">
        html += getPageHtml(updateData) + "  ";
        </shiro:hasPermission>
        <shiro:hasPermission name="oa:hr:position:delete">
        html += getDeleteHtml(deleteData) + "  ";
        </shiro:hasPermission>
        return html;
    }
    function updateZTree() {
        $.ajax({
            url: "${basePath}/oa/hr/position/getAllPosition",
            type: "GET",
            dataType: "JSON",
            async: false,
            success: function (treeNodes) {
                zNodes = treeNodes;
            }
        });
    }

    //todo把方法添加进去
    function refreshTree() {
        var rootNode = treeObj.getNodes()[0];
        if (checkedNode) {
            rootNode.checkedNodeId = checkedNode.id;
            treeObj.reAsyncChildNodes(rootNode, "refresh");
        }
    }
    var setting = {
        async: {
            enable: true,
            url: "${basePath}/oa/hr/position/getAllPosition",//异步加载时的请求地址
            type: 'get',
            dataType: 'json',
            autoParam: ["checkedNodeId", "id"]
        },
        data: {
            simpleData: {
                enable: true
            }
        },
        callback: {
            onClick: onClick
        }
    };
    function onClick(event, treeId, treeNode, clickFlag) {
        query.id = treeNode.id;
        $('#table').bootstrapTable('refresh', {query: query});
        checkedNode = treeObj.getSelectedNodes()[0];
    }
    function getChecked() {
        var treeObj = $.fn.zTree.getZTreeObj("aside_tree");
        var nodes = treeObj.getSelectedNodes();
        if (nodes < 1) {
            return "";
        }
        return Common.getParam({pid: nodes[0].id});
    }
</script>
</body>
</html>